<?php
/**
 * This is User model file. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil.models
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
/**
 * User class
 *
 * @uses          LilAppModel
 * @package       lil
 * @subpackage    lil.models
 */
class User extends LilAppModel {
/**
 * name property
 *
 * @var string 'User'
 * @access public
 */
	var $name = 'User';
/**
 * displayField property
 *
 * @var string 'name'
 * @access public
 */
	var $displayField = 'name';
/**
 * recursive property
 *
 * @var int
 * @access public
 */
	var $recursive = -1;
/**
 * validate property
 *
 * @var array
 * @access public
 */
	var $validate = array(
		'name'  => array(
			'rule'     => array('minLength', '1'),
			'required' => true
		),
		'repeat_pass' => array(
			'rule'       => 'testRepeatPassword',
			'required'   => false,
		),
		'old_pass' => array(
			'rule'       => 'testOldPassword',
			'required'   => false,
		),
	);
/**
 * components property
 *
 * @var array
 * @access public
 */
	var $components = array('Session', 'Auth', 'Security');
/**
 * testRepeatPassword method
 *
 * @param mixed $check
 * @access private
 * @return void
 */
	function testRepeatPassword($check) {
		$value = array_values($check);
		$value = $value[0];
		if (empty($this->data['User']['passwd']) || 
			(Security::hash('', null, true) == $this->data['User']['passwd'])) {
			return true;
		}
		return isset($this->data['User']['passwd']) &&
			$this->data['User']['passwd'] == $value;
	}
/**
 * testOldPassword method
 *
 * @param mixed $check
 * @access private
 * @return void
 */
	function testOldPassword($check) {
		$value = array_values($check);
		$value = $value[0];
		// return true on empty password (do nothing on empty password)
		if (empty($this->data['User']['passwd']) || 
			(Security::hash('', null, true) == $this->data['User']['passwd'])) {
			return true;
		}
		
		if (isset($this->data['User']['id'])) {
			return $this->hasAny(
				array(
					$this->primaryKey => $this->data['User'][$this->primaryKey],
					'User.passwd'     => Security::hash($value, null, true)
				)
			);
		}
		return false;
	}
/**
 * beforeSave callback
 *
 * @access public
 * @return boolean
 */
	function beforeSave() {
		if (isset($this->data['User']['passwd'])) {
			if (
				empty($this->data['User']['passwd']) ||
				($this->data['User']['passwd'] == Security::hash('', null, true))
			) {
				unset($this->data['User']['passwd']);
			} else {
				$this->data['User']['passwd'] = Security::hash($this->data['User']['passwd'], null, true);
			}
		}
		
		return parent::beforeSave();
	}
/**
 * __construct method
 *
 * @param mixed $id
 * @param mixed $table
 * @param mixed $ds
 * @access private
 * @return void
 */
	function __construct($id = false, $table = null, $ds = null) {
		$this->displayField = Configure::read('Lil.userDisplayField');
		$this->useTable = Configure::read('Lil.usersTable');
		
		if (
			(Configure::read('Lil.userDisplayField') != 'name') && 
			isset($this->validate['name']) && 
			!isset($this->validate[Configure::read('Lil.userDisplayField')])
		) {
			
			$this->validate[Configure::read('Lil.userDisplayField')] = $this->validate['name'];
			unset($this->validate['name']);
		}
		
		// associate user with areas
		if (!Configure::read('Lil.noAreas')) {
			$assoc = Configure::read('Lil.userAssociation');
			$this->hasAndBelongsToMany['Area'] = array_merge($assoc, array('className' => 'Lil.Area'));
		}
		
		parent::__construct($id, $table, $ds);
	}
/**
 * canAccessArea method
 *
 * @param mixed $user_id
 * @access public
 * @return boolean
 */
	function canAccessArea($user_id = null, $area_id = null) {
		if (!empty($user_id) || ($user_id = $this->user('id'))) {
			if ($this->isAdmin($user_id)) return true;
			$AreasUser =& ClassRegistry::init('Lil.AreasUser');
			
			// there is no AreasUser table so return true
			if (empty($AreasUser->useTable)) return true;
			
			$assoc = Configure::read('Lil.userAssociation');
			return $AreasUser->hasAny(array(
				$assoc['foreignKey']            => $user_id,
				$assoc['associationForeignKey'] => $area_id
			));
		} else return false;
	}
}